import * as mongoose from 'mongoose';
import * as dayjs from 'dayjs';

const { Schema } = mongoose;

const fastFormatTimeSchema = (
  keys: any[] = ['createdAt', 'updatedAt'],
  format: string = 'YYYY-MM-DD hh:mm:ss',
): {
  [key: string]: {
    type: Date;
    default: number;
    timezone: string;
    get(): string;
  };
} => {
  const res: any = {};
  for (const key of keys) {
    res[key] = {
      type: Date,
      default: Date.now() + 8 * 60 * 60 * 1000,
      timezone: 'Asia/Shanghai',
      get(val: Date): string {
        return dayjs(val).format(format);
      },
    };
  }
  return res;
};

export const noIdSchema = (opts: any) => {
  const schema: mongoose.Schema = new Schema(opts, {
    _id: false,
  });
  return schema;
};

export default fastFormatTimeSchema;
